package wnhz.com.business.forget;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.CountDownTimerUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;

public class ForgetPasswordActivity extends BaseActivity implements View.OnClickListener, ActionBarClickListener {
    @BindView(R.id.tv_yanzhengma)
    TextView tv_yanzhengma;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.ed_phone)
    EditText et_phone;
    @BindView(R.id.bt_next)
    Button bt_next;
    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;

    private String phone, code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        actionbar.setData("", R.drawable.ic_lefft_white, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        initView();
    }

    private void initView() {
        bt_next.setOnClickListener(this);
        tv_yanzhengma.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_next:
                phone = et_phone.getText().toString();
                code = et_code.getText().toString();
                checkInput();

                break;
            case R.id.tv_yanzhengma:
                String phone = et_phone.getText().toString();
                if (!ConfirmUtils.isMobileNO(phone)) {
                    Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                } else {
                    uploadYanZheng(phone);
                    CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_yanzhengma, 60000, 1000);
                    mCountDownTimerUtils.start();
                }
                break;
            default:
                break;
        }

    }

    private void checkInput() {
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!ConfirmUtils.isMobileNO(phone)) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        yanZhengMaVeri(phone, code);

    }


    private void uploadYanZheng(String mobile) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", 3);

        XUtil.Post(Url.HUOQUYANZHENGMA, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("==获取= ", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {

                    }
                    MyToast(jsonObject.optString("info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

        });
    }


    private void yanZhengMaVeri(final String mobile, final String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("type", 3);
        map.put("code", code);

        XUtil.Post(Url.USER_VERIFICATION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("==获取= ", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.optString("re"))) {
                        //成功
                        startActivity(ForgetPasswordResetActivity.createIntent(ForgetPasswordActivity.this, code, mobile));
                    }
                    MyToast(jsonObject.optString("info"));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

        });
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }
}
